﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using KimPhongCRM.Models;

namespace KimPhongCRM.Controllers
{
    [PermissionFilter]
    public class RoleMenuController : Controller
    {
        private Entities db = new Entities();

        //
        // GET: /RoleMenu/

        public ViewResult Index()
        {
            var tblrolemenus = db.tblRoleMenus.Include(t => t.Role).Include(t => t.tblMainMenu);
            return View(tblrolemenus.ToList());
        }

        //
        // GET: /RoleMenu/Details/5

        public ViewResult Details(Guid id)
        {
            tblRoleMenu tblrolemenu = db.tblRoleMenus.Find(id);
            return View(tblrolemenu);
        }

        //
        // GET: /RoleMenu/Create

        public ActionResult Create()
        {
            ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName");
            ViewBag.MenuId = new SelectList(db.tblMainMenus, "MenuId", "MenuSuffix");
            return View();
        } 

        //
        // POST: /RoleMenu/Create

        [HttpPost]
        public ActionResult Create(tblRoleMenu tblrolemenu)
        {
            if (ModelState.IsValid)
            {
                //tblrolemenu.MenuId = Guid.NewGuid();
                db.tblRoleMenus.Add(tblrolemenu);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName", tblrolemenu.RoleId);
            ViewBag.MenuId = new SelectList(db.tblMainMenus, "MenuId", "MenuSuffix", tblrolemenu.menuId);
            return View(tblrolemenu);
        }
        
        //
        // GET: /RoleMenu/Edit/5
 
        public ActionResult Edit(Guid id)
        {
            tblRoleMenu tblrolemenu = db.tblRoleMenus.Find(id);
            ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName", tblrolemenu.RoleId);
            ViewBag.MenuId = new SelectList(db.tblMainMenus, "MenuId", "MenuSuffix", tblrolemenu.menuId);
            return View(tblrolemenu);
        }

        //
        // POST: /RoleMenu/Edit/5

        [HttpPost]
        public ActionResult Edit(tblRoleMenu tblrolemenu)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblrolemenu).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.RoleId = new SelectList(db.Roles, "RoleId", "RoleName", tblrolemenu.RoleId);
            ViewBag.MenuId = new SelectList(db.tblMainMenus, "MenuId", "MenuSuffix", tblrolemenu.menuId);
            return View(tblrolemenu);
        }

        //
        // GET: /RoleMenu/Delete/5
 
        public ActionResult Delete(Guid id)
        {
            tblRoleMenu tblrolemenu = db.tblRoleMenus.Find(id);
            return View(tblrolemenu);
        }

        //
        // POST: /RoleMenu/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(Guid id)
        {            
            tblRoleMenu tblrolemenu = db.tblRoleMenus.Find(id);
            db.tblRoleMenus.Remove(tblrolemenu);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}